﻿using HuaCheng.DataAccess;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using HuaCheng.Controller;
using HuaCheng.CommonClass;
namespace HuaCheng.Views
{
    /// <summary>
    /// 单位定制
    /// </summary>
    public partial class UnitCustom : Form
    {
        //ini中存储的物理量及对应单位
        Dictionary<string, string> dicCustom = IniControl.ReadCustom("unit custom");
        //定义一个委托在关闭时更新通道物理量单位
        public delegate void updateUnit();
        //委托对应事件
        public event updateUnit AfterColse;
        public UnitCustom()
        {
            InitializeComponent();
            LoadUnit();
        }
        /// <summary>
        /// 监听方法
        /// </summary>
        public void OnUpdateUnit()
        {
            if (AfterColse != null)
            {
                AfterColse();
            }
        }        
        /// <summary>
        /// 再次打开单位定制窗口时，显示上一次设置的单位
        /// </summary>
        private void LoadUnit()
        {
            foreach (string key in dicCustom.Keys)
            {
                string strComboxName = "cbx" + key;
                ComboBox cbxControl = (ComboBox)this.gbxCustom.Controls[strComboxName];
                cbxControl.Text = dicCustom[key];
            }
            if (IniControl.ReadMode() == "True")
            {
                rbtnDefault.Checked = true;
            }
            else 
            {
                rbtnCustom.Checked = true;
            }
        }
        /// <summary>
        /// 默认单位设置单选按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnDefault_CheckedChanged(object sender, EventArgs e)
        {
            foreach (string key in dicCustom.Keys)
            {
                string strComboxName = "cbx" + key;
                ComboBox cbxControl = (ComboBox)this.gbxCustom.Controls[strComboxName];
                cbxControl.SelectedIndex = 0;
                cbxControl.Enabled = false;
            }
        }
        /// <summary>
        /// 定制单位设置单选按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnCustom_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Control cbxControl in this.gbxCustom.Controls)
            {
                if (cbxControl is ComboBox)
                {
                    cbxControl.Enabled = true;
                }
            }
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string strSec="unit custom";
            string path = DicHelper.RetrieveConfigPath();
            foreach (Control cbxControl in this.gbxCustom.Controls)
            {
                if (cbxControl is ComboBox)
                { 
                    string key=cbxControl.Name.Substring(3);
                    string value = cbxControl.Text;
                    ReadAndWriteIni.WritePrivateProfileString(strSec, key, value, path);
                }
            }
            string keyMode = "Mode";
            string valueMode = rbtnDefault.Checked.ToString();
            ReadAndWriteIni.WritePrivateProfileString(strSec, keyMode, valueMode, path);
            OnUpdateUnit();//开始监听
            this.Close();
        }

        #region 国际化
        private void UnitCustom_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("UnitSetting");
            this.rbtnDefault.Text = ResourceCulture.GetString("DefaultSetting");
            this.rbtnCustom.Text = ResourceCulture.GetString("SettingUnit");
            this.cbxAllapply.Text = ResourceCulture.GetString("ApplyToAllDevices");
            this.gbxCustom.Text = ResourceCulture.GetString("Order");
            this.lblVoltage.Text = ResourceCulture.GetString("VoltUnit") + ":";
            this.lblCurrent.Text = ResourceCulture.GetString("CurUnit") + ":";
            this.lblCap.Text = ResourceCulture.GetString("CapUnit") + ":";
            this.lblTemp.Text = ResourceCulture.GetString("TempUnit") + ":";
            this.lblRes.Text = ResourceCulture.GetString("RUnit") + ":";
            this.lblPower.Text = ResourceCulture.GetString("PowerUnit") + ":";
            this.lblTime.Text = ResourceCulture.GetString("TimeUnit") + ":";
            this.lblEnergy.Text = ResourceCulture.GetString("EngUnit") + ":";
            this.lblPressure.Text = ResourceCulture.GetString("PressureUnit") + ":";
            this.lblStepTime.Text = ResourceCulture.GetString("StepUnit") + ":";
            this.lblCommonTime.Text = ResourceCulture.GetString("CommonUnit") + ":";
            this.btnConfirm.Text = ResourceCulture.GetString("SureText") + ":";
            this.btnCancle.Text = ResourceCulture.GetString("LoginCancel") + ":";
        }
        #endregion

    }
}
